package com.wenhao.newcode.shopee;

import java.util.LinkedList;

/**
 * @author Wenhao Tong
 * @create 2021-07-17 12:54
 */
public class Main3 {
    public static void main(String[] args) {
        Main3 main3 = new Main3();
        String s = main3.computeString("3*[a2*[c]]");
        System.out.println(s);
    }
    /**
     *
     * @param str string字符串
     * @return string字符串
     */
    public String computeString (String str) {
        StringBuilder result = new StringBuilder();
        LinkedList<Integer> stackNum = new LinkedList<>();
        LinkedList<String> stackStr = new LinkedList<>();
        int i = 0;
        char c = str.charAt(0);
        while (i < str.length()) {
            if (c >= '0' && c <= '9') {
                int num = 0;
                while (c >= '0' && c <= '9') {
                    num = num * 10 + c - '0';
                    c = str.charAt(i);
                    i++;
                }
                stackNum.push(num);
            } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                String temp = "";
                while ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    temp += c;
                    c = str.charAt(i);
                    i++;
                }
                stackStr.push(temp);
            } else if (c == ']') {
                int k = stackNum.pop();
                String s = "";
                String temp = stackStr.pop();
                for (int j = 0;j < k;j++) {
                    s += temp;
                }
                stackStr.push(stackStr.pop() + s);
            }
            i++;
        }
        return stackStr.pop();
    }
}
